package com.hjb.service.controller.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hjb.service.cms.CmsArticleDataService;
import com.hjb.service.cms.CmsArticleService;
import com.hjb.service.cms.CmsLinkService;
import com.hjb.service.common.BannerService;
import com.hjb.service.common.CmsContentService;
import com.hjb.service.config.constant.Constants;
import com.hjb.service.config.properties.WxProperties;
import com.hjb.service.domain.cmsarticle.CmsArticle;
import com.hjb.service.domain.cmsarticle.CmsArticleExample;
import com.hjb.service.domain.cmsarticledata.CmsArticleData;
import com.hjb.service.domain.cmsarticledata.CmsArticleDataExample;
import com.hjb.service.domain.cmslink.CmsLink;
import com.hjb.service.domain.cmslink.CmsLinkExample;
import com.hjb.service.product.ProductService;
import com.hjb.service.utils.DateUtil;
import com.hjb.service.utils.MapUtil;

/**
 * 首页 Controller
 * @author zhangxl
 * @date 2017-07-20
 * @version 1.0
 */
@RestController
@RequestMapping("/common")
public class HomeController {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ProductService productService;
	@Autowired
	private BannerService bannerService;
	@Autowired
	private CmsContentService contentService;
	@Autowired
	private CmsLinkService cmsLinkService;
	@Autowired
	private CmsArticleService cmsArticleService;
	@Autowired
	private CmsArticleDataService cmsArticleDataService;
	@Autowired
	private WxProperties wxProperties;
	
	/**
	 * App首页接口
	 * @param request
	 * @return
	 */
	@RequestMapping("/home")
	@ResponseBody
	public Map<String, Object> homeList(HttpServletRequest request){
		Map<String,Object> dataMap = new HashMap<String, Object>();
		Map<String,Object> resMap =MapUtil.getRetMap();
		try {
			Map<String,Object> params = new HashMap<String, Object>();
			//定期产品列表
			params.put("type", "1");
			List<Map<String, Object>> productList = productService.getProductList(params);
			dataMap.put("productList", productList);
			
			//活期产品
			params = new HashMap<String, Object>();
			params.put("type", "0");
			productList = productService.getProductList(params);
			dataMap.put("product", productList.get(0));
			
			//体验标
			params = new HashMap<String, Object>();
			params.put("type", "2");
			productList = productService.getProductList(params);
			dataMap.put("experiProduct", productList.get(0));
			
			//公告列表
			/*List<Map<String,Object>> noticeList = contentService.getContentList(params);
			for(Map<String,Object> notice:noticeList){
				notice.put("ntcContent", "");
			}*/
			List<Object> noticeList = new ArrayList<Object>();
//			Page<CmsArticle> page = PageHelper.startPage(currentPage, pageSize, true);
			CmsArticleExample cmsArticleExample = new CmsArticleExample();
			cmsArticleExample.createCriteria().andCategoryIdEqualTo("3").andDelFlagEqualTo("0")/*.andRemarksEqualTo(Constants.BANNER_CHANNEL_APP)*/;
			cmsArticleExample.setOrderByClause("weight desc,update_date desc");
			List<CmsArticle> cmsArticleList = cmsArticleService.selectByExample(cmsArticleExample);
			if (cmsArticleList != null && cmsArticleList.size() > 0) {
				for (CmsArticle cmsArticle : cmsArticleList) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("ntcId", cmsArticle.getId());
					map.put("ntctitle", cmsArticle.getTitle());
					map.put("ntcContent", cmsArticle.getDescription() != null ? cmsArticle.getDescription() : "");
//					map.put("ntcImg", cmsArticle.getImage() != null ? wxProperties.getImg_url() + cmsArticle.getImage() : "");
					map.put("ntcImg", cmsArticle.getImage() != null ? cmsArticle.getImage() : "");
					map.put("author", cmsArticle.getKeywords() != null ? cmsArticle.getKeywords() : "");
					map.put("ntcDate", DateUtil.format(cmsArticle.getCreateDate(), DateUtil.FORMAT_LONG));
					map.put("subjectName", "网站公告");
					noticeList.add(map);
					CmsArticleDataExample cmsArticleDataExample = new CmsArticleDataExample();
					cmsArticleDataExample.createCriteria().andIdEqualTo(cmsArticle.getId());
					List<CmsArticleData> cmsArticleDataList = cmsArticleDataService.selectByExample(cmsArticleDataExample);
					if (cmsArticleDataList != null && cmsArticleDataList.size() > 0) {
						map.put("ntcContent", cmsArticleDataList.get(0).getContent());
					}
				}
			}
			
			//banner列表
			/*params = new HashMap<String, Object>();
			params.put("channel", Constants.BANNER_CHANNEL_APP);
			List<Map<String,Object>> bannerList = bannerService.getBannerList(params);*/
			List<Object> bannerList = new ArrayList<Object>();
			CmsLinkExample cmsLinkExample = new CmsLinkExample();
			cmsLinkExample.createCriteria().andDelFlagEqualTo("0").andRemarksEqualTo(Constants.BANNER_CHANNEL_APP)/*.andCategoryIdEqualTo("1")*/;
			cmsLinkExample.setOrderByClause("weight desc,update_date desc");
			List<CmsLink> cmsLinkList = cmsLinkService.selectByExample(cmsLinkExample);
			if (cmsLinkList != null && cmsLinkList.size() > 0) {
				for (CmsLink cmsLink : cmsLinkList) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("bannerName", cmsLink.getTitle());
					map.put("linkUrl", cmsLink.getHref());
//					map.put("iconUrl", wxProperties.getImg_url() + cmsLink.getImage());
					map.put("iconUrl", cmsLink.getImage());
					bannerList.add(map);
				}
			}
			
			//菜单列表
			Map<String,Object> menuMap = new HashMap<String, Object>();
			menuMap.put("risk", "http://new.ycqjewelry.com/activity/go_activity_treasure_box_page.htm");
			menuMap.put("guide", "http://new.ycqjewelry.com/activity/go_activity_steal_envelop_page.htm");
			menuMap.put("about", "http://new.ycqjewelry.com/activity/go_activity_steal_envelop_page.htm");
			
			dataMap.put("noticeList", noticeList);
			dataMap.put("bannerList", bannerList);
			dataMap.put("menuList", menuMap);
			resMap.put("data", dataMap);
			
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");	
		}
		
		return resMap;
	}
}
